package com.us.usws.batch;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import net.iwsh.core.common.GlobalConfig;
import net.iwsh.core.db.DBExecutor;
import net.iwsh.core.exception.BadResourceException;
import net.iwsh.core.utils.ResourceHelper;

import org.apache.commons.io.FileUtils;

import com.us.usws.biz.ResourceStorageReplacer;

public class DefaultResouceFixer {

	private static final String SQL_GET_CHANNEL_IDS = "select uid from hv_channel";
	private static final String SQL_UPDATE_CHANNEL_PIC = "update hv_channel set picture=? where uid=?";
	private static final String SQL_GET_VIDEO_FILE_IDS = "select uid from hv_video_file";
	private static final String SQL_UPDATE_VIDEO_FILE_RES = "update hv_video_file set snapshot=? where uid=?";
	private static final String SQL_GET_USER_IDS = "select uid from sys_user";
	private static final String SQL_UPDATE_USER_AVATAR = "update sys_user set avatar=? where uid=?";

	private DBExecutor dbe = new DBExecutor();

	/**
	 * init
	 * @throws BadResourceException
	 */
	private static void init() throws BadResourceException {
		String conf = "etc/config/global.conf.xml";
		InputStream is = ResourceHelper.getResourceAsStream(conf);
		GlobalConfig.buildConfig(is);
	}

	/**
	 * fix resources
	 * @throws BadResourceException
	 * @throws IOException
	 */
	public void fix() throws BadResourceException, IOException {
		//1. fix channel pictures
		{
			String channelPicDft = "etc/default-resource/channel-picture-default.png";
			File dftFile = new File(channelPicDft);

			List<String> uids = dbe.listWithOneColumn(SQL_GET_CHANNEL_IDS, 1);
			dbe.startTransaction();
			for (String each : uids) {
				String picName = each + ".png";
				dbe.executeBatch(SQL_UPDATE_CHANNEL_PIC, picName, each);

				String picPath = ResourceStorageReplacer.getChannelPicPath(each, picName);
				//String picPath = picDir + "/" + picName;

				//copy
				File destFile = new File(picPath);
				if (!destFile.exists()) {
					FileUtils.copyFile(dftFile, destFile);
				}
			}
			dbe.commitTransaction();
		}
		//2. fix video files
		{
			String videoSnapshotDft = "etc/default-resource/video-snapshot-default.png";
			String videoFileDft = "etc/default-resource/video-file-default.mp4";
			File dftSnapshotFile = new File(videoSnapshotDft);
			File dftFileFile = new File(videoFileDft);
			List<String> uids = dbe.listWithOneColumn(SQL_GET_VIDEO_FILE_IDS, 1);
			dbe.startTransaction();
			for (String each : uids) {
				String picName = each + ".png";
				//String videoName = each + ".mp4";
				dbe.executeBatch(SQL_UPDATE_VIDEO_FILE_RES, picName, each);

				//copy
				String snapshotPath = ResourceStorageReplacer.getVideoFileSnapshotPath(each, picName);
				String videoPath = ResourceStorageReplacer.getVideoFilePath(each, "mp4");
				File destSnap = new File(snapshotPath);
				File destVideo = new File(videoPath);

				if (!destSnap.exists()) {
					FileUtils.copyFile(dftSnapshotFile, destSnap);
				}
				if (!destVideo.exists()) {
					FileUtils.copyFile(dftFileFile, destVideo);
				}
			}
			dbe.commitTransaction();

		}

		//3. fix user avatar
		{
			String avatarDft = "etc/default-resource/avatar-default.png";
			File dftFile = new File(avatarDft);

			List<String> uids = dbe.listWithOneColumn(SQL_GET_USER_IDS, 1);
			dbe.startTransaction();
			for (String each : uids) {
				String picName = each + ".png";
				dbe.executeBatch(SQL_UPDATE_USER_AVATAR, picName, each);

				String picPath = ResourceStorageReplacer.getUserAvatarPath(each, picName);

				//copy
				File destFile = new File(picPath);
				if (!destFile.exists()) {
					FileUtils.copyFile(dftFile, destFile);
				}
			}
			dbe.commitTransaction();
		}

	}

	/**
	 * main method
	 * @param args
	 * @throws BadResourceException
	 * @throws IOException 
	 */
	public static void main(String[] args) throws BadResourceException, IOException {
		init();

		DefaultResouceFixer me = new DefaultResouceFixer();
		me.fix();

	}
}
